package com.homedo.odin.solution.service;

import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import com.dangdang.ddframe.rdb.sharding.id.generator.self.IPIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.ColumnResourceResponseBean;
import com.homedo.odin.solution.contract.mng.SpecialColumnDetailResponseBean;
import com.homedo.odin.solution.contract.mng.request.column.SpecialColumnListRequestBean;
import com.homedo.odin.solution.contract.mng.response.column.SpecialColumnListResponseBean;
import com.homedo.odin.solution.contract.mng.response.column.SpecialColumnPageResponseBean;
import com.homedo.odin.solution.contract.pc.PcColumnListRequestBean;
import com.homedo.odin.solution.contract.pc.PcSpecialColumnPageResponseBean;
import com.homedo.odin.solution.contract.pc.SpecialColumnPcDetailResponseBean;
import com.homedo.odin.solution.dao.BrandFirmMapper;
import com.homedo.odin.solution.dao.BrandOwnerMapper;
import com.homedo.odin.solution.dao.ColumnResourceMapper;
import com.homedo.odin.solution.dao.ColumnTagMapper;
import com.homedo.odin.solution.dao.ExpertProfileMapper;
import com.homedo.odin.solution.dao.SpecialColumnMapper;
import com.homedo.odin.solution.dao.UserBasicMapper;
import com.homedo.odin.solution.dao.UserColumnMapper;
import com.homedo.odin.solution.enumcode.BrandStatusEnum;
import com.homedo.odin.solution.enumcode.UserTypeEnum;
import com.homedo.odin.solution.enumcode.basic.CollectionEnum;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.enumcode.basic.DownloadCollectEnum;
import com.homedo.odin.solution.enumcode.basic.IsUseEnum;
import com.homedo.odin.solution.enumcode.column.ColumnTypeEnum;
import com.homedo.odin.solution.enumcode.column.SpecialColumnStatusEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionStateEnum;
import com.homedo.odin.solution.model.entity.BrandFirm;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.model.entity.ColumnResource;
import com.homedo.odin.solution.model.entity.ColumnTag;
import com.homedo.odin.solution.model.entity.ExpertProfile;
import com.homedo.odin.solution.model.entity.SpecialColumn;
import com.homedo.odin.solution.model.entity.UserBasic;
import com.homedo.odin.solution.model.entity.UserColumn;
import com.homedo.odin.solution.model.request.mng.column.ColumnResourceRequestBean;
import com.homedo.odin.solution.model.request.mng.column.SpecialColumnRequestBean;
import com.homedo.odin.solution.model.request.pc.brand.PcBrandColumnListRequestBean;
import com.homedo.odin.solution.model.request.pc.collect.CollectColumnRequestBean;
import com.homedo.odin.solution.model.response.pc.column.BrandColumnListResponseBean;
import com.homedo.odin.solution.model.response.pc.column.SpecialColumnFiveListResponseBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p> 栏目 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class SpecialColumnServiceImpl extends BaseService<SpecialColumnMapper, SpecialColumn> {

    @Autowired
    private ExpertProfileMapper expertProfileMapper;
    @Autowired
    private BrandFirmMapper brandFirmMapper;
    @Autowired
    private ColumnTagMapper columnTagMapper;
    @Autowired
    private ColumnResourceMapper columnResourceMapper;
    @Autowired
    private BrandFirmServiceImpl brandFirmService;
    @Autowired
    private BrandOwnerMapper brandOwnerMapper;
    @Autowired
    private ColumnTagServiceImpl columnTagService;
    @Autowired
    private UserColumnMapper userColumnMapper;
    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;
    @Autowired
    private SpecialColumnMapper specialColumnMapper;
    @Autowired
    private UserBasicMapper userBasicMapper;
    /*****************************张磊Start*****************************/

    /**
     * OMS_新增栏目
     */
    @Transactional(rollbackFor = Exception.class)
    public void addColumn(SpecialColumnRequestBean requestBean) {
        SpecialColumn column = new SpecialColumn();

        getExpertName(requestBean, column);

        if (ColumnTypeEnum.SPECIAL_COLUMN.getDbValue().equals(requestBean.getColumnType())) {
            if (StringUtils.isBlank(requestBean.getColumnText())) {
                throw new BizException(Meta.CODE_451.getCode(), "专栏栏目columnText不能为空");
            }
            //专栏所需正文内容
            column.setColumnText(requestBean.getColumnText());
        } else {
            if (CollectionUtils.isEmpty(requestBean.getResourceList())) {
                throw new BizException(Meta.CODE_451.getCode(), "视频栏目资源信息不能为空");
            }
            //视频资源所需数据
            column.setVideoPic(requestBean.getVideoPic());
            column.setVideoIntroduction(requestBean.getVideoIntroduction());
            column.setColumnVideo(requestBean.getColumnVideo());
        }

        column.setColumnType(requestBean.getColumnType());
        column.setMatchPic(requestBean.getMatchPic());

        ColumnTag columnTag = columnTagMapper.selectById(requestBean.getTagId());
        if (columnTag == null) {
            throw new BizException(Meta.CODE_451.getCode(), "栏目标签不存在 tagId:" + requestBean.getTagId());
        }
        //修改标签为可用状态，OMS暂时没有删除标签的功能，应该是默认为0
        if (!IsUseEnum.YES.getDbValue().equals(columnTag.getIsUse())) {
            columnTag.setIsUse(IsUseEnum.YES.getDbValue());
            columnTag.setLastModifiedTime(new Date());
            columnTagMapper.updateById(columnTag);
        }

        column.setTagId(requestBean.getTagId());
        column.setColumnTitle(requestBean.getColumnTitle());
        column.setColumnIntroduction(requestBean.getColumnIntroduction());
        column.setEditorId(requestBean.getEditorId());
        column.setEditorName(requestBean.getEditorName());
        //未发布状态
        column.setStatus(SpecialColumnStatusEnum.UNPUBLISHED.getDbValue());

        IdGenerator uuidGenerator = new IPIdGenerator();
        column.setColumnId((Long) uuidGenerator.generateId());
        baseMapper.insert(column);

        Long columnId = column.getColumnId();
        //视频栏目资源
        if (ColumnTypeEnum.VIDEO_COLUMN.getDbValue().equals(requestBean.getColumnType())) {
            List<ColumnResourceRequestBean> resourceList = requestBean.getResourceList();
            resourceList.forEach(resource -> resource.setColumnId(columnId.longValue()));
            columnResourceMapper.insertList(resourceList);
        }
    }

    /**
     * OMS_修改栏目详情
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateSpecialColumn(SpecialColumnRequestBean requestBean) {
        SpecialColumn specialColumn = baseMapper.getSpecialColumn(requestBean.getColumnId());
        if (specialColumn == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目不存在");
        }

        ColumnTag tag = columnTagMapper.selectByTagId(requestBean.getTagId(), DeleteEnum.NOT_DEL.getDbValue());
        if (tag == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该标签不存在或已删除");
        }

        getExpertName(requestBean, specialColumn);

        //标签已重新选择，之前的标签需要判断是否还有在使用的，没有则修改未使用
        if (!requestBean.getTagId().equals(specialColumn.getTagId())) {
            List<SpecialColumn> specialColumnList = baseMapper.queryColumnByTagId(specialColumn.getTagId());
            if (CollectionUtils.isEmpty(specialColumnList)) {
                columnTagMapper.setColumnTagStatus(specialColumn.getTagId(), IsUseEnum.NO.getDbValue());
            } else {
                //排除当前栏目并筛选正在使用该标签的栏目，没有则修改标签未使用
                List<SpecialColumn> useTagColumnList = specialColumnList.stream()
                        .filter(bean -> !bean.getColumnId().equals(requestBean.getColumnId())).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(useTagColumnList)) {
                    columnTagMapper.setColumnTagStatus(specialColumn.getTagId(), IsUseEnum.NO.getDbValue());
                }
            }
        }

        //新绑定的标签
        specialColumn.setTagId(requestBean.getTagId());
        //标签设置已使用状态
        tag.setIsUse(IsUseEnum.YES.getDbValue());
        tag.setLastModifiedTime(new Date());
        columnTagMapper.updateById(tag);

        //Spring拷贝对象
        org.springframework.beans.BeanUtils.copyProperties(requestBean, specialColumn);
        specialColumn.setLastModifiedTime(new Date());
        baseMapper.updateById(specialColumn);
        if (CollectionUtils.isNotEmpty(requestBean.getResourceList())) {
            //删除之前的资源
            columnResourceMapper.updateDelByColumnId(DeleteEnum.IS_DEL.getDbValue(), specialColumn.getColumnId());
            //视频栏目资源
            if (ColumnTypeEnum.VIDEO_COLUMN.getDbValue().equals(requestBean.getColumnType())) {
                List<ColumnResourceRequestBean> resourceList = requestBean.getResourceList();
                //修改之前数据被修改的部分
                List<ColumnResourceRequestBean> updateList = resourceList.stream()
                        .filter(bean -> bean.getResourceId() != 0).collect(Collectors.toList());
                updateList.forEach(bean -> {
                    ColumnResource resource = new ColumnResource();
                    resource.setId(bean.getResourceId());
                    resource.setResourceTitle(bean.getResourceTitle());
                    resource.setResourceUrl(bean.getResourceUrl());
                    columnResourceMapper.updateById(resource);
                });

                //新增数据
                List<ColumnResourceRequestBean> insertList = resourceList.stream()
                        .filter(bean -> bean.getResourceId() == 0).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(insertList)) {
                    resourceList.forEach(resource -> resource.setColumnId(specialColumn.getColumnId()));
                    columnResourceMapper.insertList(insertList);
                }
            }
        }
    }


    /**
     * OMS_查询栏目列表
     */
    public SpecialColumnPageResponseBean querySpecialColumnListByParams(SpecialColumnListRequestBean requestBean) {
        PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize());
        PageInfo<SpecialColumn> pageInfo = new PageInfo<>(
                baseMapper.querySpecialColumnListMngByParams(requestBean.getColumnTitle(), requestBean.getExpertName(),
                        requestBean.getColumnType(), requestBean.getCreateStartTime(), requestBean.getCreateEndTime(),
                        requestBean.getStatus()));

        SpecialColumnPageResponseBean responseBean = new SpecialColumnPageResponseBean();
        if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
            List<SpecialColumnListResponseBean> responseBeanList = pageInfo.getList().stream()
                    .filter(bean -> bean != null)
                    .map(bean -> BeanUtils.transfrom(SpecialColumnListResponseBean.class, bean))
                    .collect(Collectors.toList());
            responseBean.setList(responseBeanList);
        }

        responseBean.setPageNum(pageInfo.getPageNum());
        responseBean.setPages(pageInfo.getPages());
        responseBean.setPageSize(pageInfo.getPageSize());
        responseBean.setTotal(pageInfo.getTotal());
        return responseBean;
    }

    /**
     * OMS_查看栏目详情
     */
    public SpecialColumnDetailResponseBean specialColumnDetail(Long columnId) {
        SpecialColumn specialColumn = baseMapper.getSpecialColumn(columnId);
        if (specialColumn == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目不存在");
        }

        SpecialColumnDetailResponseBean responseBean = BeanUtils
                .transfrom(SpecialColumnDetailResponseBean.class, specialColumn);

        //获取栏目资源
        if (ColumnTypeEnum.VIDEO_COLUMN.getDbValue().equals(specialColumn.getColumnType())) {
            List<ColumnResource> columnResourceList = columnResourceMapper
                    .selectByColumnId(columnId, DeleteEnum.NOT_DEL.getDbValue());
            responseBean.setResourceList(columnResourceList);
        }

        //判断作者身份信息
        ExpertProfile one = new ExpertProfile();
        one.setId(specialColumn.getExpertId());
        ExpertProfile expert = expertProfileMapper.selectOne(one);
        responseBean.setAuthorship(UserTypeEnum.COMMON_USER.getKey());
        if (expert != null) {
            responseBean.setAuthorship(UserTypeEnum.EXPERT_USER.getKey());
        } else {
            BrandFirm brandFirm = brandFirmMapper.getBrandFirm(specialColumn.getExpertId());
            if (brandFirm != null) {
                responseBean.setAuthorship(UserTypeEnum.BRAND_USER.getKey());
            }
        }
        return responseBean;
    }

    /**
     * OMS_发布/删除 栏目
     */
    @Transactional(rollbackFor = Exception.class)
    public void publishSpecialColumn(Long columnId, Integer status, Long userId, String opretor) {
        if (!SpecialColumnStatusEnum.dbValueExist(status)) {
            throw new BizException(Meta.CODE_451.getCode(), "操作状态非法 20：发布 30：删除");
        }

        if (!NumberHelper.validate(userId)) {
            throw new BizException(Meta.CODE_451.getCode(), "操作用户ID非法");
        }

        SpecialColumn specialColumn = baseMapper.getSpecialColumn(columnId.longValue());
        if (specialColumn == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目不存在");
        }

        if (SpecialColumnStatusEnum.DELETED.getDbValue().equals(specialColumn.getStatus())) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目已经删除");
        }

        specialColumn.setStatus(SpecialColumnStatusEnum.ALREADY_RELEASED.getDbValue());
        specialColumn.setLastModifiedTime(new Date());
        specialColumn.setStatus(status);
        specialColumn.setEditorId(userId.toString());
        specialColumn.setEditorName(opretor);
        baseMapper.updateById(specialColumn);
        //删除操作
        if (SpecialColumnStatusEnum.DELETED.getDbValue().equals(status)) {
            //视频栏目 删除资源
            if (ColumnTypeEnum.VIDEO_COLUMN.getDbValue().equals(specialColumn.getColumnType())) {
                columnResourceMapper.updateByColumnId(specialColumn.getColumnId(), DeleteEnum.IS_DEL.getDbValue());
            }
        }
    }


    /**
     * PC_获取栏目信息列表
     */
    public PcSpecialColumnPageResponseBean selectSpecialColumnList(PcColumnListRequestBean requestBean) {

        //栏目类型（1.专栏栏目2.视频栏目）
        Integer columnType = requestBean.getColumnType();
        if (columnType != null) {
            if (!ColumnTypeEnum.dbValueExist(columnType)) {
                throw new BizException(Meta.CODE_451.getCode(), "columnType 非法 1专栏 2视频");
            }
        }

        //品牌主ID
        Integer brandFirmUserId = requestBean.getBrandFirmUserId();
        String tagName = requestBean.getTagName();
        Set<Integer> tagIds = null;
        //品牌主查询栏目数据
        if (NumberHelper.validate(brandFirmUserId)) {
            BrandFirm brandFirm = brandFirmService.getBrandFirmByUserId(brandFirmUserId);
            //数据必须是审核通过的品牌主
            if (brandFirm == null || brandFirm.getStatus().intValue() != SolutionStateEnum.AUDITSUCCESS.getDbValue()) {
                throw new BizException(Meta.CODE_451.getCode(), "该品牌厂商不存在或未审核通过");
            }
            //校验该品牌是否存在
            BrandOwner brandOwner = brandOwnerMapper.getBrandOwnerById(brandFirm.getBrandId());
            if (brandOwner == null) {
                throw new BizException(Meta.CODE_451.getCode(), "该品牌不存在或已删除");
            }
        } else if (StringUtils.isNotBlank(tagName)) {
            //标签名称查询栏目数据
            List<ColumnTag> tags = columnTagMapper
                    .selectByName(tagName, IsUseEnum.YES.getDbValue(), DeleteEnum.NOT_DEL.getDbValue());
            if (CollectionUtils.isEmpty(tags)) {
                throw new BizException(Meta.CODE_451.getCode(), "标签不存在");
            }
            tagIds = tags.stream().map(bean -> bean.getId()).collect(Collectors.toSet());
        }

        //品牌主Id以及标签Id都不存在，查询全部发布的
        PageHelper.startPage(requestBean.getCurrentPage(), requestBean.getPageSize());
        PageInfo<SpecialColumn> pageInfo = new PageInfo<>(baseMapper
                .selectByTagIdsOrExpertIdOrColumnTypeOrStatus(tagIds, brandFirmUserId, columnType,
                        SpecialColumnStatusEnum.ALREADY_RELEASED.getDbValue()));
        return BeanUtils.transfrom(PcSpecialColumnPageResponseBean.class, pageInfo);
    }


    /**
     * PC_获取栏目详细信息
     */
    public SpecialColumnPcDetailResponseBean querySpecialColumnDetail(Long columnId, Integer userId) {
        SpecialColumn specialColumn = baseMapper.getSpecialColumn(columnId);
        if (specialColumn == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目不存在");
        }

        SpecialColumnPcDetailResponseBean responseBean = BeanUtils
                .transfrom(SpecialColumnPcDetailResponseBean.class, specialColumn);

        //前一个栏目
        Long lastColumnId = baseMapper.queryLastSpecialColumn(specialColumn.getCreateTime());
        if (NumberHelper.validate(lastColumnId)) {
            responseBean.setLastColumnId(lastColumnId);
        }

        //下一个栏目
        Long nextSpecialColumn = baseMapper.queryNextSpecialColumn(specialColumn.getCreateTime());
        if (NumberHelper.validate(nextSpecialColumn)) {
            responseBean.setNextColumnId(nextSpecialColumn);
        }

        responseBean.setIsCollect(CollectionEnum.NOT_COLLECTION.getDbValue());
        if (NumberHelper.validate(userId)) {
            UserColumn userColumn = userColumnMapper
                    .getUserSpecialColumn(userId, columnId, DownloadCollectEnum.COLLECTION.getDbValue());
            //收藏未收藏
            if (userColumn != null && DownloadCollectEnum.COLLECTION.getDbValue()
                    .equals(userColumn.getCollectionStatus())) {
                responseBean.setIsCollect(CollectionEnum.COLLECTION.getDbValue());
            }
        }

        //增加栏目商品sku 原代码有该业务，核对页面没看到有需要展示SKU的，所以去掉了
        //判断作者身份 判断该用户是否为专家用户,赋值用户类型   原代码逻辑，暂时去掉了，感觉没用到

        //如果是视频栏目，需要资源链接等
        addColumnResource(responseBean);

        specialColumn.setPageView(specialColumn.getPageView() + 1);
        specialColumn.setLastModifiedTime(new Date());
        baseMapper.updateById(specialColumn);
        return responseBean;
    }


    /**
     * PC_点赞栏目
     */
    public void likeSpecialColumn(Long columnId) {
        SpecialColumn specialColumn = baseMapper.getSpecialColumn(columnId);
        if (specialColumn == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目不存在");
        }
        if (!SpecialColumnStatusEnum.ALREADY_RELEASED.getDbValue().equals(specialColumn.getStatus())) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目没有发布成功");
        }
        baseMapper.addSpecialColumnLike(columnId);
    }

    /**
     * PC_收藏栏目
     */
    @Transactional(rollbackFor = Exception.class)
    public void collectSpecialColumn(CollectColumnRequestBean requestBean) {
        SpecialColumn specialColumn = baseMapper.getSpecialColumn(requestBean.getColumnId());
        if (specialColumn == null) {
            throw new BizException(Meta.CODE_451.getCode(), "该栏目不存在");
        }

        UserColumn userColumn = userColumnMapper
                .getUserSpecialColumn(requestBean.getUserId(), requestBean.getColumnId(),
                        DownloadCollectEnum.COLLECTION.getDbValue());
        if (CollectionEnum.COLLECTION.getDbValue().equals(requestBean.getStatus())) {
            //新增收藏记录
            if (userColumn == null) {
                //新增收藏记录
                UserColumn uColumn = new UserColumn();
                uColumn.setActionType(DownloadCollectEnum.COLLECTION.getDbValue());
                uColumn.setColumnId(requestBean.getColumnId());
                uColumn.setColumnType(specialColumn.getColumnType());
                uColumn.setCollectionStatus(DeleteEnum.NOT_DEL.getDbValue());
                uColumn.setUserId(requestBean.getUserId());
                userColumnMapper.insert(uColumn);
            } else if (userColumn != null) {
                //改变已删除的收藏记录 ,变为正常
                if (DeleteEnum.IS_DEL.getDbValue().equals(userColumn.getCollectionStatus())) {
                    userColumn.setCollectionStatus(DeleteEnum.NOT_DEL.getDbValue());
                    userColumnMapper.updateById(userColumn);
                }
            } else {
                //已收藏
                throw new BizException(Meta.CODE_451.getCode(), "当前用户已经收藏了该栏目");
            }
            //收藏量+1
            baseMapper.addSpecialColumnCollectCount(requestBean.getColumnId());
        } else {
            //取消收藏
            if (userColumn == null || DeleteEnum.IS_DEL.getDbValue().equals(userColumn.getCollectionStatus())) {
                throw new BizException(Meta.CODE_451.getCode(), "收藏数据不存在或已删除!");
            }
            userColumn.setCollectionStatus(DeleteEnum.IS_DEL.getDbValue());
            userColumn.setLastModifiedTime(new Date());
            userColumnMapper.updateById(userColumn);
        }
    }

    /**
     * 获取当前前五个往期栏目信息
     */
    public List<SpecialColumnFiveListResponseBean> querySpecialColumnFiveList(Long columnId, Integer columnType) {
        SpecialColumn specialColumn = baseMapper.getSpecialColumn(columnId);
        if (specialColumn == null) {
            throw new BizException(Meta.CODE_451.getCode(), "当前栏目不存在");
        }

        List<SpecialColumn> dbList = baseMapper
                .querySpecialColumnListByCreateTime(columnType, specialColumn.getCreateTime());
        if (CollectionUtils.isEmpty(dbList)) {
            return null;
        }

        return dbList.stream().filter(bean -> bean != null)
                .map(bean -> BeanUtils.transfrom(SpecialColumnFiveListResponseBean.class, bean))
                .collect(Collectors.toList());
    }

    /**
     * 视频栏目获取资源
     */
    private void addColumnResource(SpecialColumnPcDetailResponseBean responseBean) {
        List<ColumnResource> dbList = columnResourceMapper
                .selectByColumnId(responseBean.getColumnId(), DeleteEnum.NOT_DEL.getDbValue());
        List<ColumnResourceResponseBean> responseList = dbList.stream().filter(bean -> bean != null)
                .map(bean -> BeanUtils.transfrom(ColumnResourceResponseBean.class, bean)).collect(Collectors.toList());
        responseBean.setResourceList(responseList);
    }


    /**
     * 获取栏目作者信息
     */
    private void getExpertName(SpecialColumnRequestBean requestBean, SpecialColumn specialColumn) {
        if (NumberHelper.validate(requestBean.getExpertId())) {
            ExpertProfile one = new ExpertProfile();
            one.setUid(requestBean.getExpertId());
            ExpertProfile expert = expertProfileMapper.selectOne(one);

            if (expert == null) {
                BrandFirm brandFirm = brandFirmMapper.getBrandFirm(requestBean.getExpertId());
                if (brandFirm == null) {
                    specialColumn.setExpertId(requestBean.getExpertId());
                    specialColumn.setExpertName(requestBean.getExpertName());
                }else{
                    specialColumn.setExpertId(brandFirm.getUserId());
                    specialColumn.setExpertName(brandFirm.getUserName());
                }
            } else {
                specialColumn.setExpertId(expert.getUid());
                specialColumn.setExpertName(expert.getName());
            }
        } else {
            specialColumn.setExpertId(requestBean.getExpertId());
            specialColumn.setExpertName(requestBean.getExpertName());
        }
    }

    /*****************************张磊end*****************************/

    public List<BrandColumnListResponseBean> getColumnsByUser(Integer userId,
            PcBrandColumnListRequestBean requestBean) {
        PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize());
        List<BrandColumnListResponseBean> responseList = new ArrayList<BrandColumnListResponseBean>();
        BrandFirm brandFirm = brandFirmService.getBrandFirmByUserId(userId);
        if (brandFirm == null || brandFirm.getStatus().intValue() != BrandStatusEnum.AUDITSUCCESS.getKey()) {
            throw new BizException(Meta.CODE_451.getCode(), "该品牌厂商不存在!");
        }
        BrandOwner brandOwner = brandOwnerService.getBrandOwnerById(brandFirm.getBrandId());
        if (brandOwner == null) {
            throw new BizException(Meta.CODE_451.getCode(), "关联品牌主不存在!");
        }
        PageInfo<SpecialColumn> pageInfo = new PageInfo<>(specialColumnMapper.queryColumnListForBrandFirm(userId,
                requestBean.getColumnType()));
        List<SpecialColumn> queryList = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(queryList)) {
            queryList.forEach(specialColumn -> {
                BrandColumnListResponseBean columnResponse = new BrandColumnListResponseBean();
                BeanUtils.copyProperties(specialColumn, columnResponse);
                ColumnTag columnTag = columnTagService.getColumnTag(specialColumn.getTagId());
                if (columnTag != null) {
                    columnResponse.setTagName(columnTag.getName());
                } else {
                    columnResponse.setTagName("");
                }
                responseList.add(columnResponse);
            });
        }

        return responseList;
    }

    public SpecialColumn getSpecialColumn(Long columnId) {
        return baseMapper.getSpecialColumn(columnId);
    }

    public void addSpecialColumnCommentCount(Long columnId) {
        baseMapper.addSpecialColumnCommentCount(columnId);
    }


}
